import { Component, OnInit, Input } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { Validators, FormBuilder, ValidationErrors, AbstractControl, FormGroup, FormControl } from '@angular/forms';
import { Router,NavigationExtras } from '@angular/router';
import { CommonSelectedBankPage } from '@common-components/common-selected-bank/common-selected-bank.page';
import { EncryptService } from '@services/encrypt.service';
import { HttpClient } from "@angular/common/http";
import { AuthTokenService } from '@services/auth-token.service';
import {CustomToastProviderService} from '@common-components/common-toast/custom-toast-provider.service';

@Component({
  selector: 'app-common-payment-password',
  templateUrl: './common-payment-password.page.html',
  styleUrls: ['./common-payment-password.page.scss'],
})
export class CommonPaymentPasswordPage implements OnInit {
  @Input()payment:boolean;

  @Input()url:string;

  @Input()payAmt:any;
  @Input()payMethod:any;
  @Input()txnCcy:any;
  @Input()cashAmt:any;
  @Input()loanAmt:any;
  @Input()signature:any;
  @Input()createDate:any;
  @Input()txnAmt:any;
  @Input()invoiceNo:any;
  @Input()creditId:any;
  @Input()installment:any;
  @Input()billingStatement:any;
  @Input()qrCodeId:any;

  paymentPasswordForm!: FormGroup

  constructor(
    public modalCtrl: ModalController,
    private fb: FormBuilder,
    private encryptService:EncryptService,
    private http:HttpClient,
    private authToken : AuthTokenService,
    private customToast:CustomToastProviderService,
    private router:Router
  ) {
    this.paymentPasswordForm = this.fb.group({
      paymentPasswod:new FormControl('',{  validators:Validators.required })
    });
  }

  get paymentPasswod(){
    return this.paymentPasswordForm.get('paymentPasswod');
  }

  ngOnInit() {
  }

  
  dismissModal() {
    this.modalCtrl.dismiss({
      'dismissed': true
    });
  }

  appUserId = this.authToken.getAppUserId();
  paymentPasswodSubmit(){
    let pwd = this.encryptService.md5Ecrypt(this.paymentPasswordForm.value.paymentPasswod);
    // let url = '/scm/app/checkOldPwd.do';
    // {
    //   appUserId: this.appUserId,//this.authTokenService.getAppUserId(),
    //   paymentPwd:pwd
    // }
    let url = '/scm/app/paymentOrder.do';
    debugger
    let requestData = {
      payAmt:this.payAmt,
      payMethod:this.payMethod,
      txnCcy:this.txnCcy,
      txnAmt:this.txnAmt,
      cashAmt:this.cashAmt,
      loanAmt:this.loanAmt,
      signature:this.signature,
      createDate:this.createDate,
      invoiceNo:this.invoiceNo,
      creditId:this.creditId,
      paymentPassword:pwd,
      installment:{},
      billingStatement:{},
      appUserId:this.appUserId,
      qrCodeId:this.qrCodeId
    }

    this.http.post(url, requestData).subscribe((response:any) => {
      if(!response.responseBody.checkSuccess){
        // return resolve( { "oldPwderror":true } );
        let responseMsg = 'Payment Password error';
        if(response.responseBody.checkMessage){
          responseMsg=response.responseBody.checkMessage;
        }
        this.customToast.show({
          icon:false,
          iconSuccess:false,
          message:responseMsg,
          duration:2000
        });
        this.paymentPasswordForm.reset();
      }else{
        // return resolve(  { "oldPwderror":false });
        this.dismissModal();
        if(this.payment){
          this.router.navigate(['/tabs/myAccount/installment/successfully']);
        }else{
          let urlData:NavigationExtras={
            queryParams:{
              txnAmt:this.txnAmt
            }
          }
          this.router.navigate(['/tabs/home/scan/checkout/paid-success'],urlData);
        }
      }
    });
  }

  selectedBank(){
    this.modalCtrl.create({
      component: CommonSelectedBankPage,
      cssClass: 'selected_bank',
      componentProps: {

      }
    }).then(
      (result)=>{
        result.present();
      }
    );
  }

  forgotPassword(){
    this.dismissModal();
    let navigationExtras: NavigationExtras = {
      queryParams: {
       url:this.url
      } 
    };
    this.router.navigate(['/tabs/myAccount/setting/payment-settings/payment-password/password-reset'],navigationExtras);
  }
}
